package Sudoku3.structure;


import java.util.BitSet;


public class cell {

	private int _value = 0;
    private int _r;
    private int _c;
    private grid _grid;
    private BitSet _possible = new BitSet(9);
	
    //default constructor
    public cell(grid g, int r, int c) {
    	_r = r;
    	_c = c;
    	_grid = g;
    	this.resetPossible();
    }
   
    public int getR() { return _r; }
    public int getC() { return _c; }
    
    public boolean isSolved() { return (_value > 0); }
    
    public BitSet getPossible() { return _possible; }
    public boolean isPossible(int v) { return (_possible.get(v - 1)); }

    public void addPossible(int v) { _possible.set(v - 1, true); }
    public void removePossible(int v) { _possible.set(v - 1, false); }
    public void removePossible(BitSet b) { _possible.andNot(b); }
    public void resetPossible() { _possible.set(0, 9); }
    public void clearPossible() { _possible.clear(); }
    
    
    
    public int getValue() { return _value; }
    public void setValue(int v) {
    	_value = v; 
    	this.fromInt((int)Math.pow(2, v-1));
    }


    public void fromInt(int i) {
      BitSet bits = new BitSet();
      int index = 0;
      while (i != 0) {
        if (i % 2 != 0) {
          bits.set(index);
        }
        ++index;
        i = i >>> 1;
      }
      _possible = bits;
    }
    
    
    public int toInt() {
        int r = 0;
        for(int i = 0 ; i < _possible.length(); i++)
            if(_possible.get(i))
                r |= (1 << i);
        return (_value == 0) ? r : (int)Math.pow(2, _value-1);
    }
    
    public String toString() {
        String t = "";
        for (int i = 1; i < 10; i++) { t += (isPossible(i)) ? i : "."; }
        return t;
    }
    
    public String toStringCoords() { return "R" + _r + "C" + _c; }
    
    public bucket getAllNeighbors() {
    	
    	bucket b = new bucket();
    	
    	for (Class <? extends group> grouptype : _grid.getGroupTypes()) {
    		group g = _grid.getGroupAt(grouptype, _r, _c);
    		for (cell c : g) {
    			if (!(b.contains(c))) { b.add(c); }
    		}
    	}
    	b.remove(this);
    	return b;
    }
	
}
